package com.qixin.web.serviceimpl;

import com.qixin.api.bean.ComPeople;
import com.qixin.api.bean.ComPtUser;
import com.qixin.api.bean.ComStationinfo;
import com.qixin.api.bean.ComStationinfoPicture;
import com.qixin.api.service.ComStationinfoPictureService;
import com.qixin.api.service.ComStationinfoService;
import com.qixin.web.bean.WeCollection;
import com.qixin.web.mapper.WeCollectionMapper;
import com.qixin.web.service.WeCollectionService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qixin.web.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 微信 我的收藏 服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2018-10-31
 */
@Service
public class WeCollectionServiceImpl extends ServiceImpl<WeCollectionMapper, WeCollection> implements WeCollectionService {
    @Autowired
    ComStationinfoService comStationinfoService;
    @Autowired
    ComStationinfoPictureService comStationinfoPictureService;

    @Override
    public String insertMyLike(HttpSession session, Long chargeId) {
        //获取session  userId
        Long userId = SessionUtil.getUserId(session);


        //判断有无收藏过本个充电站
        Map map = new HashMap<>();
        map.put("charge_id", chargeId);
        map.put("user_id", userId);
        List<WeCollection> weCollections = this.selectByMap(map);
        if (weCollections.size() > 0) {
            return "您已收藏过该充电站，请到收藏夹中查看";
        }
        //开始收藏
        ComStationinfo comStationinfo = comStationinfoService.selectById(chargeId);
        Map map1=new HashMap<>();
        map1.put("relaction_id",comStationinfo.getId());

       List<ComStationinfoPicture>  comStationinfoPictures=comStationinfoPictureService.selectByMap(map1);

        WeCollection weCollection = new WeCollection();
        weCollection.setAddress(comStationinfo.getAddress());
        weCollection.setSimpleAddress(comStationinfo.getStationName());
        weCollection.setLat(comStationinfo.getStationLat() + "");
        weCollection.setLng(comStationinfo.getStationLng() + "");
        weCollection.setUserId(userId + "");
        weCollection.setChargeId(chargeId + "");
        weCollection.setImageUrl(comStationinfoPictures.get(0).getPicture());

        if (this.insert(weCollection)) {
            return "收藏成功";
        }
        ;
        return "出现意外错误。";
    }

    @Override
    public List<WeCollection> findMyLike(HttpSession session) {
        Long userId = SessionUtil.getUserId(session);
        Map map = new HashMap<>();
        map.put("user_id", userId);
        return this.selectByMap(map);
    }
}
